<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" title="登录模式" :width="width" @ok="handleSubmit" destroyOnClose>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button preIcon="ant-design:plus-outlined" type="primary" @click="handleAdd">新增</a-button>
      </template>
      <template #action="{ record }">
        <TableAction :actions="getActions(record)" />
      </template>
    </BasicTable>
    <IdpConnectForm @register="registerIdpConnectForm" @success="reload" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, useAttrs, onMounted, nextTick } from "vue"
  import MyAlert from "/@/components/My/MyAlert.vue"
  import { BasicDrawer, useDrawerInner } from "/@/components/Drawer"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useModal } from "/@/components/Modal"
  import { useDrawer } from "/@/components/Drawer"
  import IdpConnectForm from "./Form.vue"
  import { useGlobSetting } from "/@/hooks/setting"
  import { columns } from "./data"
  import * as extIdpApi from "/@/api/iam/extIdp"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { useMessage } from "/@/hooks/web/useMessage"

  const emit = defineEmits(["success", "register"])
  const attrs = useAttrs()

  const idpTmplId = ref("")
  const width = ref<number | string>("800px")
  // 列表页面公共参数、方法
  const { prefixCls, tableContext } = useListPage({
    designScope: "ldp-connect",
    tableProps: {
      title: "登录模式",
      api: async (params) => {
        const data = await extIdpApi.getIdpTmpl(params)
        return data?.connectTmpls
      },
      showTableSetting: false,
      showIndexColumn: false,
      bordered: false,
      striped: false,
      immediate: false,
      columns: columns,
      useSearchForm: false,
    },
  })

  // const [registerLdpFieldListModal, { openModal: openFieldListModal  }] = useModal();
  const [registerTable, { reload, setProps }] = tableContext
  const { createMessage } = useMessage()
  const glob = useGlobSetting()
  const [registerIdpConnectForm, { openDrawer: openIdpConnectForm }] = useDrawer()

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ confirmLoading: false })
    idpTmplId.value = data.record.id
    setProps({ searchInfo: { id: data.record.id } })
    reload()
  })

  async function handleSubmit() {
    try {
      closeDrawer()
      emit("success")
    } finally {
      setDrawerProps({ confirmLoading: false })
    }
  }

  /**
   * 新增事件
   */
  function handleAdd() {
    openIdpConnectForm(true, { isUpdate: false, idpTmplId: idpTmplId.value })
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await extIdpApi.deleteIdpConnectTmpl({ id: record.id }, reload)
  }

  const handleEdit = (record) => {
    openIdpConnectForm(true, { isUpdate: true, record, idpTmplId: idpTmplId.value })
  }

  /**
   * 操作列定义
   * @param record
   */
  function getActions(record) {
    return [
      {
        label: "删除",
        ifShow: record.sendStatus != 1,
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
    ]
  }
</script>

<style lang="less"></style>
